package com.horoscope.astrology.zodiac.signs.free.activity

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.ImageView
import android.widget.TextView
import com.horoscope.astrology.zodiac.signs.free.R
import com.horoscope.astrology.zodiac.signs.free.bean.LoveResultBean
import com.horoscope.astrology.zodiac.signs.free.network.NetworkEngine
import com.horoscope.astrology.zodiac.signs.free.network.NetworkListener
import com.horoscope.astrology.zodiac.signs.free.utils.ConstellationUtil
import com.horoscope.astrology.zodiac.signs.free.utils.ShareManager
import kotlinx.android.synthetic.main.activity_result.*
import kotlinx.android.synthetic.main.item_share.*
import kotlinx.android.synthetic.main.view_network_error.*

class LoveResultActivity : BaseActivity() {

    private var h1 = ""
    private var h2 = ""
    private lateinit var heart: ImageView
    private lateinit var mRate: TextView
    private lateinit var mContent: TextView
    private lateinit var you: ImageView
    private lateinit var partner: ImageView
    private lateinit var oneName: TextView
    private lateinit var otherName: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_result)

        if (intent == null) {
            return
        }

        h1 = intent.getStringExtra(YOU)
        h2 = intent.getStringExtra(PARTNER)

        initView()
        initViewData()
        request()
        activityShowAd("Interstitial_Love")
    }

    private fun initViewData() {
        val anim = AnimationUtils.loadAnimation(this, R.anim.scale_2)
        heart.startAnimation(anim)
        oneName.text = h1
        otherName.text = h2
        you.setImageResource(ConstellationUtil.getIconByName(h1))
        partner.setImageResource(ConstellationUtil.getIconByName(h2))
    }

    private fun initView() {
        initTitleBar(
            this,
            R.id.btn_back_edit_name,
            R.id.text_view_back_title,
            ""
        )
        heart = findViewById(R.id.image_view_heart)
        you = findViewById(R.id.image_view_you)
        partner = findViewById(R.id.image_view_partner)
        oneName = findViewById(R.id.text_view_single1)
        otherName = findViewById(R.id.text_view_single2)
        mRate = findViewById(R.id.text_view_rate)
        mContent = findViewById(R.id.text_view_content_love)
        // 无网络或发生其他错误
        tv_network_ok.setOnClickListener {
            include_error.visibility = View.GONE
            request()
        }
        ShareManager.initShare(
            this@LoveResultActivity,
            null,
            null,
            null,
            image_view_more_share,
            tv_facebook
        )
    }

    private fun request() {
        showLoading()
        NetworkEngine.getLoveResult(h1, h2, listener)
    }

    private val listener = object : NetworkListener<LoveResultBean>() {
        @SuppressLint("SetTextI18n")
        override fun onSuccess(t: LoveResultBean) {
            mRate.text = "${t.data.rate}%"
            mContent.text = t.data.content
            root_love_result?.visibility = View.VISIBLE
            showLoaded()
            include_error.visibility = View.INVISIBLE
        }

        override fun onFailed(t: LoveResultBean) {
            super.onFailed(t)
            errorMake()
        }

        override fun onError(e: String?) {
            super.onError(e)
            errorMake()
        }
    }

    private fun errorMake() {
        showLoaded()
        include_error.visibility = View.VISIBLE
    }

    companion object {
        private const val YOU = "you"
        private const val PARTNER = "partner"
        fun generateIntent(context: Context, youName: String, partner: String): Intent {
            val intent = Intent(context, LoveResultActivity::class.java)
            intent.putExtra(YOU, youName)
            intent.putExtra(PARTNER, partner)
            return intent
        }
    }

}